﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Wms;
using nERP.Model;
using nERP.Model.So;
using nERP.Model.Bas;

namespace nERP.ViewModel.Wms.SaleReturnInWarehouseVMs
{
    public partial class SaleReturnInWarehouseListVM : BasePagedListVM<SaleReturnInWarehouse_View, SaleReturnInWarehouseSearcher>
    {
        
        protected override IEnumerable<IGridColumn<SaleReturnInWarehouse_View>> InitGridHeader()
        {
            return new List<GridColumn<SaleReturnInWarehouse_View>>{
                
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_SaleReturn).SetTitle(@Localizer["Page.销售退货"].Value),
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_Sale).SetTitle(@Localizer["Page.销售订单"].Value),
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_Customer).SetTitle(@Localizer["Page.客户"].Value),
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_SaleReturnInWarehouseCode).SetTitle(@Localizer["Page.入库单号"].Value),
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_InvoicesDate).SetTitle(@Localizer["Page.单据日期"].Value),
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_MakeUser).SetTitle(@Localizer["Page.制单人"].Value),
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_Status).SetTitle(@Localizer["Page.单据状态"].Value),
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),
                this.MakeGridHeader(x => x.SaleReturnInWarehouse_IsValid).SetTitle(@Localizer["_Admin.IsValid"].Value),
                this.MakeGridHeader(x => "wtmtemp_CanApprove").SetHide().SetFormat((a, b) =>
                {
                    if(a.wtmtemp_CanApprove) { return "true"; }
                    else { return "false"; }
                }),
                

                this.MakeGridHeaderAction(width: 200)
            };
        }

        
        public override IOrderedQueryable<SaleReturnInWarehouse_View> GetSearchQuery()
        {
            var query = DC.Set<SaleReturnInWarehouse>()
                
                .CheckEqual(Searcher.SaleReturnId, x=>x.SaleReturnId)
                .CheckEqual(Searcher.SaleId, x=>x.SaleId)
                .CheckEqual(Searcher.CustomerId, x=>x.CustomerId)
                .CheckContain(Searcher.SaleReturnInWarehouseCode, x=>x.SaleReturnInWarehouseCode)
                .CheckBetween(Searcher.InvoicesDate?.GetStartTime(), Searcher.InvoicesDate?.GetEndTime(), x => x.InvoicesDate, includeMax: false)
                .CheckEqual(Searcher.MakeUserId, x=>x.MakeUserId)
                .CheckEqual(Searcher.Status, x=>x.Status)
                .CheckContain(Searcher.Remark, x=>x.Remark)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x=>x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x=>x.UpdateBy)
                .Select(x => new SaleReturnInWarehouse_View
                {
				    ID = x.ID,
                    
                    SaleReturnInWarehouse_SaleReturn = DC.Set<SaleReturn>().Where(z0 => z0.SaleReturnCode == x.SaleReturnId).Select(y => y.SaleReturnCode).FirstOrDefault(),
                    SaleReturnInWarehouse_Sale = DC.Set<Sale>().Where(z0 => z0.SaleCode == x.SaleId).Select(y => y.SaleCode).FirstOrDefault(),
                    SaleReturnInWarehouse_Customer = DC.Set<Customer>().Where(z0 => z0.CustomerCode == x.CustomerId).Select(y => y.CustomerName).FirstOrDefault(),
                    SaleReturnInWarehouse_SaleReturnInWarehouseCode = x.SaleReturnInWarehouseCode,
                    SaleReturnInWarehouse_InvoicesDate = x.InvoicesDate,
                    SaleReturnInWarehouse_MakeUser = DC.Set<FrameworkUser>().Where(z0 => z0.ITCode == x.MakeUserId).Select(y => y.Name).FirstOrDefault(),
                    SaleReturnInWarehouse_Status = x.Status,
                    SaleReturnInWarehouse_Remark = x.Remark,
                    SaleReturnInWarehouse_CreateTime = x.CreateTime,
                    SaleReturnInWarehouse_UpdateTime = x.UpdateTime,
                    SaleReturnInWarehouse_CreateBy = x.CreateBy,
                    SaleReturnInWarehouse_UpdateBy = x.UpdateBy,
                    SaleReturnInWarehouse_IsValid = x.IsValid,
                })
                .OrderBy(x => x.ID);
            return query;
        }

        
    public override void AfterDoSearcher()
    {
        if (Wtm.LoginUserInfo != null)
        {
            var ids = GetMyApproves();
            foreach (var item in EntityList)
            {
                var m = ids.Find(x => x.ModelID == item.GetID().ToString());
                if (m!=null)
                {
                    item.wtmtemp_CanApprove = true;
                }
            }
        }
    }


    }
    public class SaleReturnInWarehouse_View: SaleReturnInWarehouse
    {
        
        public string SaleReturnInWarehouse_SaleReturn { get; set; }
        public string SaleReturnInWarehouse_Sale { get; set; }
        public string SaleReturnInWarehouse_Customer { get; set; }
        public string SaleReturnInWarehouse_SaleReturnInWarehouseCode { get; set; }
        public DateTime? SaleReturnInWarehouse_InvoicesDate { get; set; }
        public string SaleReturnInWarehouse_MakeUser { get; set; }
        public StatusTypeEnum? SaleReturnInWarehouse_Status { get; set; }
        public string SaleReturnInWarehouse_Remark { get; set; }
        public DateTime? SaleReturnInWarehouse_CreateTime { get; set; }
        public DateTime? SaleReturnInWarehouse_UpdateTime { get; set; }
        public string SaleReturnInWarehouse_CreateBy { get; set; }
        public string SaleReturnInWarehouse_UpdateBy { get; set; }
        public bool SaleReturnInWarehouse_IsValid { get; set; }
                public bool wtmtemp_CanApprove { get; set; }

    }

}